class AdminController < ApplicationController
  layout "layouts/admin_layout"
  auto_complete_for :person, :last_first
  def index
    redirect_to :action=>"menu" if session[:event_id]
      
    
    @conferences = Conference.find(:all)
  end
  
  def menu
    
  end
  
  def set_event_id
    session[:event_id] = params[:id]
    redirect_to :action=>"menu"
  end
  
  def edit_person
    if params[:id]
      @person = Person.find(params[:id])
    else
      @person = Person.new
    end
  end
  
  def update_person
    unless params[:person][:id].empty?
      @person = Person.find(params[:person][:id])
    else
      @person = Person.new
    end
    @person.username = params[:person][:username]
    @person.first_name = params[:person][:first_name]
    @person.middle_name = params[:person][:middle_name]
    @person.last_name = params[:person][:last_name]
    @person.email = params[:person][:email]
    @person.organization = params[:person][:organization]
    @person.title = params[:person][:title]
    @person.save
  end
  
  def list_people
    @people = Person.find(:all, :order => "last_name, first_name, middle_name, email")
  end
  
  def edit_presentation
    if params[:id]
      @presentation = Presentation.find(params[:id])
    else
      @presentation = Presentation.new
    end
    @people = Person.find(:all, :order => "last_name, first_name, middle_name, email")
  end  
  
  def list_presentations
    @presentations = Presentation.find(:all, :conditions=>["event_id = ? AND type = ?", session[:event_id].to_i, "Presentation"])
  end
  
  def update_presentation
    unless params[:presentation][:id].empty?
      @presentation = Presentation.find(params[:presentation][:id])
    else
      @presentation = Presentation.new
    end
    @presentation.name = params[:presentation][:name]
    @presentation.description = params[:presentation][:description]
    @presentation.event_id = session[:event_id]
    @presentation.save
    @presentation.clear_role('Presenter')
    params[:person].each do | person, on |
      @presentation.participants.create({:person_id => person, :role=>'Presenter'})
    end
  end
  
  def unset_event_id
    session[:event_id] = nil
    redirect_to :action=>"index"
  end
  def edit_election
    if params[:id]
      @election = Election.find(params[:id])
    else
      @election = Election.new
    end
  end 
  
  def update_election
    unless params[:election][:id].empty?
      @election = Election.find(params[:election][:id])
    else
      @election = Election.new
    end
    @election.name = params[:election][:name]
    @election.type = params[:type]
    @election.start_time = params[:election][:start_time]
    @election.end_time = params[:election][:end_time]
    @election.conditions = params[:election][:conditions]
    @election.event_id = session[:event_id]
    @election.save
  end
  
  def list_elections
    @event = Event.find(session[:event_id])    
  end
  
  def list_location_proposals
    @locations = LocationProposal.find(:all, :conditions=>["event_id = ? and type = ?", session[:event_id].to_i, 'LocationProposal'])
  end
  
  def edit_location_proposal
    if params[:id]
      @location = LocationProposal.find(params[:id])
    else
      @location = LocationProposal.new
    end
  end  
  
  def update_location_proposal
    unless params[:location_proposal][:id].empty?
      @location = LocationProposal.find(params[:location_proposal][:id])
    else
      @location = LocationProposal.new
    end
    @location.name = params[:location_proposal][:name]
    @location.description = params[:location_proposal][:description]
    @location.event_id = session[:event_id]
    @location.save  
  end

  def list_keynote_proposals
    @keynotes = KeynoteProposal.find(:all, :conditions=>["event_id = ? and type = ?", session[:event_id].to_i, 'KeynoteProposal'])
  end

  def edit_keynote_proposal
    if params[:id]
      @keynote = KeynoteProposal.find(params[:id])
    else
      @keynote = KeynoteProposal.new
    end
  end

  def update_keynote_proposal
    unless params[:keynote_proposal][:id].empty?
      @keynote = KeynoteProposal.find(params[:keynote_proposal][:id])
    else
      @keynote = KeynoteProposal.new
    end
    @keynote.name = params[:keynote_proposal][:name]
    @keynote.description = params[:keynote_proposal][:description]
    @keynote.event_id = session[:event_id]
    @keynote.save
  end

  def list_tshirt_proposals
    @designs = TShirtProposal.find(:all, :conditions=>["event_id = ? and type = ?", session[:event_id].to_i, 'TShirtProposal'])
  end

  def edit_tshirt_proposal
    if params[:id]
      @design = TShirtProposal.find(params[:id])
    else
      @design = TShirtProposal.new
    end
  end

  def update_tshirt_proposal
    unless params[:tshirt_proposal][:id].empty?
      @design = TShirtProposal.find(params[:tshirt_proposal][:id])
    else
      @design = TShirtProposal.new
    end
    @design.name = params[:tshirt_proposal][:name]
    @design.description = params[:tshirt_proposal][:description]
    @design.event_id = session[:event_id]
    @design.save
  end

end
